﻿using CommunityToolkit.Mvvm.Input;
using HslCommunication.Profinet.Siemens;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Animation;
using ZoneLineNew.Common;
using ZoneLineNew.Core;
using ZoneLineNew.Core.Extensions;
using ZoneLineNew.Enums;
using ZoneLineNew.Models;
using ZoneLineNew.Models.Dto;
using ZoneLineNew.Models.Entity;
using ZoneLineNew.Models.Entity.BaseInfo;
using ZoneLineNew.Models.StationConfig;
using ZoneLineNew.Models.StationResult.HOPStation;
using ZoneLineNew.Models.StationResult.QOPStation;
using ZoneLineNew.Services.DB;
using ZoneLineNew.Services.Message;
using ZoneLineNew.Services.Station;
using ZoneLineNew.ViewModelBase;
using ZoneLineNew.Views.Dialogs;

namespace ZoneLineNew.ViewModels {

    public class MainViewModel : NavigationViewModel {

        private readonly IDbOperate<IFreeSqlMainContext> _mainDbOperate = null;

        public MainViewModel(
            IRegionManager regionManager,
            IDialogService dialog,
            IMessageService messageBox,
            IEventAggregator eventAggregator,
            IDbOperate<IFreeSqlMainContext> mainDbOperate,
            IWorkStationService<QOP006StationResult, QOP006StationConfig> workStationQOP006Service,
            IWorkStationService<QOP020StationResult, QOP020StationConfig> workStationQOP020Service,
            IWorkStationService<QOP010StationResult, QOP010StationConfig> workStationQOP010Service,
            IWorkStationService<QOP050LStationResult, QOP050LStationConfig> workStationQOP050LService,
            IWorkStationService<QOP050RStationResult, QOP050RStationConfig> workStationQOP050RService,
            IWorkStationService<COP610StationResult, COP610StationConfig> workStationCOP610RService
            ) : base(regionManager, dialog, messageBox, eventAggregator, mainDbOperate) {

            this._mainDbOperate = mainDbOperate;

            if (Cache.IsQopStaion) {
                WorkStationOP006Service = workStationQOP006Service;
                WorkStationOP020Service = workStationQOP020Service;
                WorkStationOP010Service = workStationQOP010Service;
                WorkStationOP050LService = workStationQOP050LService;
                WorkStationOP050RService = workStationQOP050RService;
                WorkStationOP610Service = workStationCOP610RService;
                WorkStationOP006Service.Connect();
                WorkStationOP020Service.Connect();
                WorkStationOP010Service.Connect();
                WorkStationOP050LService.Connect();
                WorkStationOP050RService.Connect();
                WorkStationOP610Service.Connect();
            }
        }


        #region Props
        private bool _isCheckOK = false;

        public bool IsCheckOK {
            get { return _isCheckOK; }
            set { _isCheckOK = value; RaisePropertyChanged(); }
        }

        #region 前减
        private IWorkStationService<QOP006StationResult, QOP006StationConfig> _workStationOP006Service;

        public IWorkStationService<QOP006StationResult, QOP006StationConfig> WorkStationOP006Service {
            get { return _workStationOP006Service; }
            set { _workStationOP006Service = value; base.RaisePropertyChanged(); }
        }

        private IWorkStationService<QOP020StationResult, QOP020StationConfig> _workStationOP020Service;

        public IWorkStationService<QOP020StationResult, QOP020StationConfig> WorkStationOP020Service {
            get { return _workStationOP020Service; }
            set { _workStationOP020Service = value; base.RaisePropertyChanged(); }
        }


        private IWorkStationService<QOP010StationResult, QOP010StationConfig> _workStationOP010Service;

        public IWorkStationService<QOP010StationResult, QOP010StationConfig> WorkStationOP010Service {
            get { return _workStationOP010Service; }
            set { _workStationOP010Service = value; base.RaisePropertyChanged(); }
        }


        private IWorkStationService<QOP050LStationResult, QOP050LStationConfig> _workStationOP050LService;

        public IWorkStationService<QOP050LStationResult, QOP050LStationConfig> WorkStationOP050LService {
            get { return _workStationOP050LService; }
            set { _workStationOP050LService = value; base.RaisePropertyChanged(); }
        }


        private IWorkStationService<QOP050RStationResult, QOP050RStationConfig> _workStationOP050RService;

        public IWorkStationService<QOP050RStationResult, QOP050RStationConfig> WorkStationOP050RService {
            get { return _workStationOP050RService; }
            set { _workStationOP050RService = value; base.RaisePropertyChanged(); }
        }

        private IWorkStationService<COP610StationResult, COP610StationConfig> _workStationOP610Service;

        public IWorkStationService<COP610StationResult, COP610StationConfig> WorkStationOP610Service
        {
            get { return _workStationOP610Service; }
            set { _workStationOP610Service = value; base.RaisePropertyChanged(); }
        }
        #endregion

        private string _customer = "产品未选择";

        public string Customer {
            get { return _customer; }
            set { _customer = value; base.RaisePropertyChanged(); }
        }

        private string _model = "型号未选择";

        public string Model {
            get { return _model; }
            set { _model = value; base.RaisePropertyChanged(); }
        }

        private string _workDirection = "方向未选择";

        public string WorkDirection {
            get { return _workDirection; }
            set { _workDirection = value; base.RaisePropertyChanged(); }
        }
        #endregion

        #region Commands

        public DelegateCommand SelectProductCommand => new DelegateCommand(() => {
            DialogParameters dialogParameters = new DialogParameters();
            dialogParameters.Add("Title", "产品选择");
            _dialog.ShowDialog(nameof(SelectProductDialogView), dialogParameters, (rhs) => {
                IsCheckOK = rhs.Result == ButtonResult.OK;
                if (IsCheckOK) {
                    Customer = Cache.CurWorkProduct.CustomerName;
                    Model = Cache.CurWorkProduct.Model;
                    WorkDirection = Cache.CurWorkProduct.WorkDirection.GetDescription();
                    WorkStationOP050LService.GetCurrentLableIndex(ePrintStartionCode.Q_OP580_1);
                    WorkStationOP050RService.GetCurrentLableIndex(ePrintStartionCode.Q_OP580_2);
                }
            });
        });

        #endregion




        protected override void OnViewLoaded() {
          

            //base.OnViewLoaded();
        }




       

    }
}
